package com.wlhse.service.impl;

import com.github.pagehelper.PageHelper;
import com.wlhse.dao.CheckRecordDao;
import com.wlhse.dto.CheckRecordRectRecordDto;
import com.wlhse.dto.getDto.ChangQinEmployeeDto;
import com.wlhse.dto.inDto.*;
import com.wlhse.dto.outDto.*;
import com.wlhse.entity.*;
import com.wlhse.exception.WLHSException;
import com.wlhse.service.CheckRecordService;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class CheckRecordServiceImpl implements CheckRecordService {

    @Value("${RESOURCES_URL}")
    private String url;

    @Resource
    private CheckRecordDao checkRecordDao;

    @Resource
    private TreeUtil treeUtil;

    private void setDateChangQin(CheckRecordInDto checkRecordInDto) {
        if (StringUtils.isNotBlank(checkRecordInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
    }

    //问题查询--新的查询
    @Override
    public String queryCheckRecordByCondition(CheckRecordQueryInDto checkRecordQueryInDto) {
        checkRecordQueryInDto.setUrl(url);
        if (StringUtils.isNotBlank(checkRecordQueryInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordQueryInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordQueryInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
        int length = checkRecordDao.getMaxLengthCompanyCode();
        int total = 0;
        List<CheckRecordPojo> list=new ArrayList<>();
        if (checkRecordQueryInDto.getCompanyCode().length()==length){//只需要查指定code
            if (checkRecordQueryInDto.getType().equals("all"))
                return NR.r(checkRecordDao.queryCheckRecordByCondition(checkRecordQueryInDto));
            //分页
            total = checkRecordDao.queryCheckRecordByConditionCount(checkRecordQueryInDto);
            PageHelper.startPage(checkRecordQueryInDto.getPageIdx(), checkRecordQueryInDto.getPageSize());
            list = checkRecordDao.queryCheckRecordByCondition(checkRecordQueryInDto);
            return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
        }else {//查询当前及子节点
            if (checkRecordQueryInDto.getType().equals("all"))
                return NR.r(checkRecordDao.queryCheckRecordChildByCondition(checkRecordQueryInDto));
            //分页
            total = checkRecordDao.queryCheckRecordChildByConditionCount(checkRecordQueryInDto);
            PageHelper.startPage(checkRecordQueryInDto.getPageIdx(), checkRecordQueryInDto.getPageSize());
            list = checkRecordDao.queryCheckRecordChildByCondition(checkRecordQueryInDto);
            return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
        }
    }

    //按条件查询---我待整改---我待验证
    @Override
    public String queryCheckRecord(CheckRecordInDto checkRecordInDto) {
        checkRecordInDto.setUrl(url);
        setDateChangQin(checkRecordInDto);
        System.out.println("最开始数据："+checkRecordInDto);
        List<CheckRecordPojo> list=new ArrayList<>();
        if (checkRecordInDto.getType().equals("all")){
            System.out.println("不分页："+checkRecordDao.getCheckRecordByCondition(checkRecordInDto));
            return NR.r(checkRecordDao.getCheckRecordByCondition(checkRecordInDto));
        }

        //分页查询
       int total = checkRecordDao.getchildCrByConditionCount(checkRecordInDto);
        System.out.println("总共条数"+total);
        PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
        list = checkRecordDao.getchildCrByCondition(checkRecordInDto);
        System.out.println("分页"+list);
        return NR.r(list, total, checkRecordInDto.getPageIdx());
    }

    //我验证的问题
    @Override
    public String queryCheckRecordMine(CheckRecordInDto checkRecordInDto) {
        //查询我验证的问题
        checkRecordInDto.setUrl(url);
        setDateChangQin(checkRecordInDto);
        int total=0;
//        List<CheckRecordPojo> list=new ArrayList<>();
//        if (checkRecordInDto.getCompanyCode().length()==length){
//            if (checkRecordInDto.getType().equals("all"))
//                return NR.r(checkRecordDao.getCheckRecordMine(checkRecordInDto));
//            //分页查询
//            total = checkRecordDao.getCheckRecordMineCount(checkRecordInDto);
//            PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
//            list = checkRecordDao.getCheckRecordMine(checkRecordInDto);
//            return NR.r(list, total, checkRecordInDto.getPageIdx());
//        }else {
//            if (checkRecordInDto.getType().equals("all"))
//                return NR.r(checkRecordDao.getCheckRecordMineChild(checkRecordInDto));
//            //分页查询
//            total = checkRecordDao.getCheckRecordMineChildCount(checkRecordInDto);
//            PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
//            list = checkRecordDao.getCheckRecordMineChild(checkRecordInDto);
//            return NR.r(list, total, checkRecordInDto.getPageIdx());
//        }

        List<CheckRecordRectRecordDto> list=new ArrayList<>();

        if (checkRecordInDto.getType().equals("all"))
            return NR.r(checkRecordDao.getCheckRecordMineChild(checkRecordInDto));
        //分页
        total = checkRecordDao.getCheckRecordMineChildCount(checkRecordInDto);
        PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
        List<CheckRecordRectRecord> res = checkRecordDao.getCheckRecordMineChild(checkRecordInDto);
        list = treeUtil.getCheckRecordMyVerifyTree(res);

        return NR.r(list, total, checkRecordInDto.getPageIdx());

    }

    @Override
    public String queryCheckRecordMineByID(int id) {
        return NR.r(CodeDict.CODE_MESSAGE_DATA, 0, 0, treeUtil.getCheckRecordMyVerifyTree(checkRecordDao.getCheckRecordMineByID(id,url)), null, 0, 0);
    }

    //我整改的问题
    @Override
    public String queryCheckRecordMineReci(CheckRecordInDto checkRecordInDto) {
        checkRecordInDto.setUrl(url);
        setDateChangQin(checkRecordInDto);
        int length = checkRecordDao.getMaxLengthCompanyCode();
        int total=0;
        List<CheckRecordRectRecordDto> list=new ArrayList<>();

        List<CheckRecordRectRecord> allRectRecord = new ArrayList<>();
        List<CheckRecordRectRecord> currentRectRecord = new ArrayList<>();
        //当前人整改的checkrecordid
//        List<RectRecordPojo> currentCheckRecord = checkRecordDao.getCurrentCheckRecord(checkRecordInDto.getSupervisorID());

        PageHelper.startPage(checkRecordInDto.getPageIdx(), checkRecordInDto.getPageSize());
        allRectRecord = checkRecordDao.getCheckRecordMineReciChild(checkRecordInDto);
        currentRectRecord=allRectRecord;
//        for (RectRecordPojo checkRecordPojo:currentCheckRecord) {//15
//            for (CheckRecordRectRecord checkRecordRectRecord:allRectRecord) {//25
//                if (checkRecordPojo.getCheckRecordID().equals(checkRecordRectRecord.getCheckRecordID())){
//                    currentRectRecord.add(checkRecordRectRecord);//31 加入了checkRecordRectRecord，但集合没有删除，会重复加入
//                }
//            }
//        }

        list = treeUtil.getCheckRecordRectMyTree(currentRectRecord);//currentRectRecord
        total = checkRecordDao.getCheckRecordMineReciChildCount2(checkRecordInDto);

        return NR.r(list, total, checkRecordInDto.getPageIdx());
    }


    //指定id查询，返回所有字段
    @Override
    public String queryCheckRecordID(int id) {
        return NR.r(CodeDict.CODE_MESSAGE_DATA, 0, 0, checkRecordDao.getCheckRecordById(id,url), null, 0, 0);
    }

    @Override
    public String deleteCheckRecord(int id) {
        if (checkRecordDao.deleteCheckRecord(id) <= 0)
            throw new WLHSException("删除失败");
        return NR.r();
    }

    //更新验证数据
    @Override
    @Transactional
    public String updateCheckRecord(CheckRecordVerifyInDto checkRecordVerifyInDto) {
        if (StringUtils.isBlank(checkRecordVerifyInDto.getVerifyDate())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordVerifyInDto.setVerifyDate(df.format(new Date()));// new Date()为获取当前系统时间
        }

        //传入未通过，重新指定整改人，更新checkrecord中除status外其他字段，新增RectRecord
        if (checkRecordVerifyInDto.getRectEffect().equals("不合格")){
            checkRecordVerifyInDto.setStatus("不合格");//验证不合格，重新指定整改人，status变成未整改
            RectRecordPojo rectRecordPojo = new RectRecordPojo();
            rectRecordPojo.setCheckRecordID(checkRecordVerifyInDto.getCheckrecordID());
            System.out.println(checkRecordVerifyInDto.getSupervisorID());
            String name = checkRecordDao.getEmplName(checkRecordVerifyInDto.getSupervisorID());
            rectRecordPojo.setSupervisorName(name);
            rectRecordPojo.setSupervisorID(checkRecordVerifyInDto.getSupervisorID());
            //查询rectRecord已有的记录改变整改效果
            List<RectRecordPojo> list = checkRecordDao.getOneRectRecord(checkRecordVerifyInDto.getCheckrecordID());
            RectRecordPojo rectRecordPojo2=new RectRecordPojo();
            for (RectRecordPojo temp:list){
                if (temp.getRectEffect()==null||temp.getRectEffect().equals(""))
                    rectRecordPojo2 = temp;
            }
            rectRecordPojo2.setRectEffect(checkRecordVerifyInDto.getRectEffect());
            checkRecordVerifyInDto.setSupervisorName(name);
            int i = 1;
            //上一条整改记录整改效果加入不通过
            i *= checkRecordDao.updateRectRecord(rectRecordPojo2);
            //新增一条整改记录
//            i *= checkRecordDao.addRectRecord(rectRecordPojo);
            CheckRecordPojo checkRecord= checkRecordDao.getCheckRecordById(checkRecordVerifyInDto.getCheckrecordID(), url);
            if(checkRecord!=null){
                String staffID=checkRecord.getCheckStaffID();
                if(staffID==null){
                    staffID="";
                }
                String verifyPersonID=checkRecordVerifyInDto.getVerifyPersonID().toString();
                if(!staffID.equals(verifyPersonID)){
                    checkRecordVerifyInDto.setStatus("未审核");
                }else{
                    checkRecordVerifyInDto.setStatus("未整改");
                }
            }
//            String id=checkRecordVerifyInDto.getId().toString();
//            if(!checkRecordById.getCheckStaffID().equals(id)){//判断是否StaffID和ID一致，不一致则修改verifyPersonID verifyPersonName
//                checkRecordVerifyInDto.setVerifyPersonID(checkRecordVerifyInDto.getId());
//                checkRecordVerifyInDto.setVerifyPersonName(checkRecordVerifyInDto.getName());
//            }
            //修改checkrecord数据
            i *= checkRecordDao.updateCheckRecord(checkRecordVerifyInDto);
            //清空checkrecord中上一个人的整改记录信息
            i*=checkRecordDao.cleanCheckRecord(checkRecordVerifyInDto.getCheckrecordID());
            if (i<=0)
                throw new WLHSException("更新失败");
            return NR.r();

        }else {//传入其他，表示通过，更新checkrecord所有字段
            //查找指定checkrecord和supervisorID的整改记录，改变整改效果
             List<RectRecordPojo> list = checkRecordDao.getOneRectRecord(checkRecordVerifyInDto.getCheckrecordID());
             RectRecordPojo rectRecordPojo=null;
             for (RectRecordPojo temp:list){
                if (temp.getRectEffect()==null||temp.getRectEffect().equals("")){
                    rectRecordPojo = temp;
                }
            }
            if (rectRecordPojo!=null)
                rectRecordPojo.setRectEffect(checkRecordVerifyInDto.getRectEffect());
            int i = 1 ;
            i *= checkRecordDao.updateRectRecord(rectRecordPojo);
            i *= checkRecordDao.updateCheckRecord(checkRecordVerifyInDto);
            if (rectRecordPojo==null||i<=0)
                throw new WLHSException("更新失败");
            return NR.r();
        }
    }

    //更新整改数据
    @Override
    @Transactional
    public String updateCheckRecordReci(CheckRecordReciInDto checkRecordReciInDto) {
        //整改一次，查询RectRecord表的当前checkrecordid对应的记录
        //没查到新增一条
        //查到，往对应RectRecord记录中添加数据,checkRecordReciInDto.getSupervisorID(),
        List<RectRecordPojo> list = checkRecordDao.getOneRectRecord(checkRecordReciInDto.getCheckrecordID());
        RectRecordPojo rectRecordPojo = new RectRecordPojo();
        for (RectRecordPojo temp:list){
            //整改数据未补充完或还未验证的数据
            if (temp.getRectDate()==null||temp.getRectDate().equals("")||temp.getRectEffect()==null||temp.getRectEffect().equals(""))
                rectRecordPojo = temp;
        }
        System.out.println(rectRecordPojo);
        if (rectRecordPojo.getCheckRecordID()==null){//新增
            rectRecordPojo.setCheckRecordID(checkRecordReciInDto.getCheckrecordID());
            String name = checkRecordDao.getEmplName(checkRecordReciInDto.getSupervisorID());
            rectRecordPojo.setSupervisorName(name);
            rectRecordPojo.setSupervisorID(checkRecordReciInDto.getSupervisorID());
            rectRecordPojo.setRectAttach1(checkRecordReciInDto.getRectAttach1());
            rectRecordPojo.setRectAttach2(checkRecordReciInDto.getRectAttach2());
            rectRecordPojo.setRectAttach3(checkRecordReciInDto.getRectAttach3());
            rectRecordPojo.setRectAttach4(checkRecordReciInDto.getRectAttach4());
            rectRecordPojo.setRectVideo(checkRecordReciInDto.getRectVideo());
            rectRecordPojo.setRectDescription(checkRecordReciInDto.getRectDescription());
            rectRecordPojo.setRectFile(checkRecordReciInDto.getRectFile());
            rectRecordPojo.setRected("是");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            rectRecordPojo.setRectDate(df.format(new Date()));
            int i = 1;
            i *= checkRecordDao.addRectRecord(rectRecordPojo);
            System.out.println(i);
            i *= checkRecordDao.updateCheckRecordReci(checkRecordReciInDto);
            System.out.println(i);
            if (i<=0)
                throw new WLHSException("更新失败");
            return NR.r();
        }else {//查到了，保存
            rectRecordPojo.setRectAttach1(checkRecordReciInDto.getRectAttach1());
            rectRecordPojo.setRectAttach2(checkRecordReciInDto.getRectAttach2());
            rectRecordPojo.setRectAttach3(checkRecordReciInDto.getRectAttach3());
            rectRecordPojo.setRectAttach4(checkRecordReciInDto.getRectAttach4());
            rectRecordPojo.setRectVideo(checkRecordReciInDto.getRectVideo());
            rectRecordPojo.setRectDescription(checkRecordReciInDto.getRectDescription());
            rectRecordPojo.setRectFile(checkRecordReciInDto.getRectFile());
            rectRecordPojo.setRected("是");
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            rectRecordPojo.setRectDate(df.format(new Date()));
            int i = 1;
            i *= checkRecordDao.updateRectRecord2(rectRecordPojo);
            i *= checkRecordDao.updateCheckRecordReci(checkRecordReciInDto);
            if (i<=0)
                throw new WLHSException("更新失败");
            return NR.r();
        }

    }

    @Override
    public String selectEmployee(ChangQinEmployeeDto changQinEmployeeDto) {
        if (changQinEmployeeDto.getType().equals("all")){
            List<ChangQinEmployeeDto> changQinEmployeeDtos = checkRecordDao.selectEmployee(changQinEmployeeDto.getName());
            return NR.r(changQinEmployeeDtos);
        }

        //分页查询
        int total = checkRecordDao.selectEmployeeCount(changQinEmployeeDto.getName());
        PageHelper.startPage(changQinEmployeeDto.getPageIdx(), changQinEmployeeDto.getPageSize());
        List<ChangQinEmployeeDto> list = checkRecordDao.selectEmployee(changQinEmployeeDto.getName());
        return NR.r(list, total, changQinEmployeeDto.getPageIdx());
    }

    //查询监督组成员
    @Override
    public String selectSuperVisionGroupMember(SuperVisionGroupMemberOutDto superVisionGroupMemberOutDto) {
        if (superVisionGroupMemberOutDto.getType().equals("all"))
            return NR.r(checkRecordDao.selectSuperVisionGroupMember(superVisionGroupMemberOutDto.getStaffName()));
        //分页查询
        int total = checkRecordDao.selectSuperVisionGroupMemberCount(superVisionGroupMemberOutDto.getStaffName());
        PageHelper.startPage(superVisionGroupMemberOutDto.getPageIdx(), superVisionGroupMemberOutDto.getPageSize());
        List<SuperVisionGroupMemberOutDto> list = checkRecordDao.selectSuperVisionGroupMember(superVisionGroupMemberOutDto.getStaffName());
        return NR.r(list, total, superVisionGroupMemberOutDto.getPageIdx());
    }

    //当前登陆人暂存问题条件查询
    @Override
    public String selectCheckRecordTemporarySave(CheckRecordTemporarySaveInDto checkRecordTemporarySaveInDto) {
        checkRecordTemporarySaveInDto.setUrl(url);
        int length = checkRecordDao.getMaxLengthCompanyCode();
        int total=0;
        List<CheckRecordTemporarySaveOutDto> list=new ArrayList<>();
        if (checkRecordTemporarySaveInDto.getCompanyCode().length()==length){
            total = checkRecordDao.getCrTemporarySaveCount(checkRecordTemporarySaveInDto);
            list = checkRecordDao.getCrTemporarySave(checkRecordTemporarySaveInDto);
        }else {
            total = checkRecordDao.getchildCrTemporarySaveCount(checkRecordTemporarySaveInDto);
            list = checkRecordDao.getchildCrTemporarySave(checkRecordTemporarySaveInDto);
        }
        if (checkRecordTemporarySaveInDto.getType().equals("all"))
            return NR.r(list);
        //分页查询
        PageHelper.startPage(checkRecordTemporarySaveInDto.getPageIdx(), checkRecordTemporarySaveInDto.getPageSize());
        return NR.r(list, total, checkRecordTemporarySaveInDto.getPageIdx());
    }

    //checkrecord和整改记录一起查询
    @Override
    public String queryCheckRecordsByCondition(CheckRecordQueryInDto checkRecordQueryInDto) {
        checkRecordQueryInDto.setUrl(url);
        if (StringUtils.isNotBlank(checkRecordQueryInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordQueryInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordQueryInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
        int length = checkRecordDao.getMaxLengthCompanyCode();
        int total = 0;
        List<CheckRecordRectRecord> list=new ArrayList<>();
        if (checkRecordQueryInDto.getCompanyCode().length()==length){//只需要查指定code
            if (checkRecordQueryInDto.getType().equals("all"))
                return NR.r(checkRecordDao.queryCheckRecordsByCondition(checkRecordQueryInDto));
            //分页
            total = checkRecordDao.queryCheckRecordsByConditionCount(checkRecordQueryInDto);
            PageHelper.startPage(checkRecordQueryInDto.getPageIdx(), checkRecordQueryInDto.getPageSize());
            list = checkRecordDao.queryCheckRecordsByCondition(checkRecordQueryInDto);
//            list = treeUtil.getCheckRecordRectTree(checkRecordDao.queryCheckRecordsByCondition(checkRecordQueryInDto));
            return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
        }else {//查询当前及子节点
            if (checkRecordQueryInDto.getType().equals("all"))
                return NR.r(checkRecordDao.queryCheckRecordsChildByCondition(checkRecordQueryInDto));
            //分页
            total = checkRecordDao.queryCheckRecordsChildByConditionCount(checkRecordQueryInDto);
            PageHelper.startPage(checkRecordQueryInDto.getPageIdx(), checkRecordQueryInDto.getPageSize());
            list = checkRecordDao.queryCheckRecordsChildByCondition(checkRecordQueryInDto);
            return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
        }
    }

    //checkrecord和整改记录一起查询
    @Override
    public String queryCheckRecordsTreeByCondition(CheckRecordQueryInDto checkRecordQueryInDto,Integer id) {
        checkRecordQueryInDto.setUrl(url);
        if (StringUtils.isNotBlank(checkRecordQueryInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordQueryInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordQueryInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
        List<CheckRecordRectRecordDto> list=new ArrayList<>();
        //查询给定code的category1
        String code = checkRecordQueryInDto.getCompanyCode();
        if (code.equals("")||code==null){//没有指定code，查询当前人id获取
            code = checkRecordDao.getCurrentCompany(id);
            checkRecordQueryInDto.setCompanyCode(code);
            CompanyPojo company = checkRecordDao.getCompanyByCode(code);
            if(company!=null&&company.getCategory1()!=null&&!company.getCategory1().equals("")){//category1不为空，按照category1查询当前及子节点
                checkRecordQueryInDto.setCompanyCode(company.getCategory1());
            }
        }
//        传入的companycode是已经筛选过的，这里不再筛选了
//        CompanyPojo company = checkRecordDao.getCompanyByCode(code);
//        if(company!=null&&company.getCategory1()!=null&&!company.getCategory1().equals("")){//category1不为空，按照category1查询当前及子节点
//            checkRecordQueryInDto.setCompanyCode(company.getCategory1());
//        }

        if (checkRecordQueryInDto.getType().equals("all")){
            // todo 这里没改，后面如果报错再改
            List<CheckRecordRectRecord> checkRecordRectRecords = checkRecordDao.queryCheckRecordsChildByCondition(checkRecordQueryInDto);
            if("2009".equals(checkRecordQueryInDto.getRoleCode())){//若请求角色为2009，排除未审核的结果
                Iterator<CheckRecordRectRecord> iterator = checkRecordRectRecords.iterator();
                while (iterator.hasNext()){
                    CheckRecordRectRecord next = iterator.next();
                    if("未审核".equals(next.getStatus())){
                        iterator.remove();
                    }
                }
            }
            return NR.r(treeUtil.getCheckRecordRectTree(checkRecordRectRecords));
        }

        //分页
        int total = checkRecordDao.queryCheckRecordsChildByConditionCount2(checkRecordQueryInDto);
        PageHelper.startPage(checkRecordQueryInDto.getPageIdx(),checkRecordQueryInDto.getPageSize());
        List<CheckRecordRectRecord> checkRecordRectRecords = checkRecordDao.queryCheckRecordsChildByCondition2(checkRecordQueryInDto);

        if("2009".equals(checkRecordQueryInDto.getRoleCode())){//若请求角色为2009，排除未审核的结果
            Iterator<CheckRecordRectRecord> iterator = checkRecordRectRecords.iterator();
            while(iterator.hasNext()){
                CheckRecordRectRecord next = iterator.next();
                if("未审核".equals(next.getStatus())){
                    iterator.remove();
                }
            }
        }

        list = treeUtil.getCheckRecordRectTree2(checkRecordRectRecords);
//给每条记录增加一个整改人岗位字段（ps:有可能一个人有两个岗位，或者姓名相同）
        for (CheckRecordRectRecordDto pojo:list) {
            EmployeePojo employeePojo=new EmployeePojo();
            employeePojo=checkRecordDao.queryStation(pojo.getSupervisorID());
            if(employeePojo!=null)pojo.setStation(employeePojo.getEmpGroup()+"|"+employeePojo.getPosition()+"|"+employeePojo.getStation());
        }
        return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
    }

    //checkrecord和整改记录一起查询
    @Override
    public String queryMyCheckRecordsTreeByCondition(CheckRecordQueryInDto checkRecordQueryInDto,Integer id) {
        checkRecordQueryInDto.setUrl(url);
        if (StringUtils.isNotBlank(checkRecordQueryInDto.getCheckDate1()) && StringUtils.isBlank(checkRecordQueryInDto.getCheckDate2())) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            checkRecordQueryInDto.setCheckDate2(df.format(new Date()));// new Date()为获取当前系统时间
        }
        int total = 0;
        List<CheckRecordRectRecordDto> list=new ArrayList<>();

        //查询给定code的category1
        String code = checkRecordQueryInDto.getCompanyCode();
        if (code.equals("")||code==null){//没有指定code，查询当前人id获取
            code = checkRecordDao.getCurrentCompany(id);
            checkRecordQueryInDto.setCompanyCode(code);
        }
        System.out.println(code);
        CompanyPojo company = checkRecordDao.getCompanyByCode(code);
        if(company!=null&&company.getCategory1()!=null){//category1不为空，按照category1查询当前及子节点
            checkRecordQueryInDto.setCompanyCode(company.getCategory1());
        }
        if (checkRecordQueryInDto.getType().equals("all"))
            return NR.r(checkRecordDao.queryCheckRecordsChildByCondition(checkRecordQueryInDto));

        //分页
        total = checkRecordDao.queryCheckRecordsChildByConditionCount(checkRecordQueryInDto);
        PageHelper.startPage(checkRecordQueryInDto.getPageIdx(),checkRecordQueryInDto.getPageSize());
        List<CheckRecordRectRecord> checkRecordRectRecords = checkRecordDao.queryCheckRecordsChildByCondition(checkRecordQueryInDto);
        list = treeUtil.getCheckRecordRectTree(checkRecordRectRecords);

        return NR.r(list, total, checkRecordQueryInDto.getPageIdx());
    }

    @Override
    public String getRoleCode(Integer id) {
        return checkRecordDao.getRoleCode(id);
    }

    @Override
    public String getRoleCode2(Integer id) {
        return NR.r(CodeDict.CODE_MESSAGE_DATA, 0, 0, checkRecordDao.getRoleCode(id), null, 0, 0);
    }

    @Override
    public String current_code_Admin(Integer id) {
        List<String> roleCodes = checkRecordDao.getRoleCodes(id);
        return NR.r(roleCodes.get(roleCodes.size()-1));
    }

    @Override
    public String current_code_Headman(Integer id) {
        List<String> roleCodes = checkRecordDao.getRoleCodes(id);
        return NR.r(roleCodes.get(0));
    }

    @Override
    public String getHazardFactors() {
        return NR.r(checkRecordDao.getHazardFactors());
    }

    @Override
    public String updateVerifyPerson(CheckRecordVerifyInDto2 checkRecordVerifyInDto) {
        //根据id得到name
        checkRecordVerifyInDto.setVerifyPersonName(checkRecordDao.getEmplName(checkRecordVerifyInDto.getVerifyPersonID()));
        if (checkRecordDao.updateVerifyPerson(checkRecordVerifyInDto) <= 0){
            throw new WLHSException("删除失败");
        }

        return NR.r();
    }
}
